home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip1091.arc / GET_DIRS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-04  |  2.6 KB  |  85 lines

  1. /*
  2. **  GET_DIRS.C - build a linked list of directories on a drive
  3. **
  4. **  public domain by Mike Gillen
  5. */
  6.  
  7. struct dirs
  8. {
  9.       char path[MAXPATH];                /* directory name         */
  10.       struct dirs *nxtdir, *prvdir;      /* next and prev pointers */
  11. };
  12.  
  13. typedef struct dirs DIRS, *DIRPTR;
  14. DIRPTR dfirst, dprev, dnow, dlast;
  15.  
  16. /*********************************************************************
  17.  
  18. get_dirs() is called to find all of the directories on the current
  19. drive.  It in turn calls gdir() after creating the first structure
  20. that coresponds to the root directory.
  21.  
  22. *********************************************************************/
  23.  
  24. void get_dirs( void )
  25. {
  26.       char work_dir[MAXPATH];
  27.  
  28.       getcwd( work_dir, MAXPATH );
  29.       chdir( "\\" );
  30.       if( (dnow = (DIRPTR) malloc( sizeof( DIRS ) ) ) == NULL )
  31.             mem_err( 52 );                /* memory error function */
  32.       dfirst = dlast = dnow->prvdir = dprev = dnow;
  33.       dnow->nxtdir = (DIRPTR) NULL;
  34.       strcpy( dnow->path, " :\\" );
  35.       dnow->path[0] = work_dir[0];
  36.       gdir( 1 );
  37.       dlast = dnow;
  38.       dnow->nxtdir = dnow;
  39.       dnow = dfirst;
  40.       dnow->prvdir = dnow;
  41.       chdir( work_dir );
  42. } /* end get_dirs() */
  43.  
  44. /*********************************************************************
  45.  
  46. gdir() uses recursion to find all of the directories on the current
  47. drive.  After building the linked list of directories, it returns
  48. to get_dirs().
  49.  
  50. *********************************************************************/
  51.  
  52. void gdir( int lev )
  53. {
  54.       struct ffblk dirinfo;
  55.  
  56.       if( findfirst( "*.*", &dirinfo, FA_DIREC | FA_HIDDEN ) )
  57.       {
  58.             chdir( ".." );
  59.             return;
  60.       }
  61.       do
  62.       {
  63.             if( !( dirinfo.ff_attrib & FA_DIREC ) )
  64.                   continue;
  65.             if( dirinfo.ff_name[0] == '.' )     /* optional to skip . dirs */
  66.                   continue;
  67.             if( (dnow = (DIRPTR) malloc( sizeof( DIRS ) ) ) == NULL )
  68.                   mem_err( 53 );                /* memory error function */
  69.             dprev->nxtdir = dnow;
  70.             dnow->prvdir = dprev;
  71.             dnow->nxtdir = (DIRPTR) NULL;
  72.             getcwd( dnow->path, MAXPATH );
  73.             strcpy( dnow->path, dnow->path );
  74.             if( dnow->path[ strlen( dnow->path ) - 1 ] != '\\' )
  75.                   strcat( dnow->path, "\\" );
  76.             strcat( dnow->path, dirinfo.ff_name );
  77.             dprev = dnow;
  78.             chdir( dnow->path );
  79.             gdir( lev + 1 );  /* recursive call */
  80.       }
  81.       while( !findnext( &dirinfo ) );
  82.       if( lev != 1 )
  83.             chdir( ".." );
  84. } /* end gdir() */
  85.